//
//  SongListViewController.m
//  GSG
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SongListViewController.h"
#import"PlayerViewController.h"
#import"AFNetworking.h"
#import"AFNetworkingTool.h"
#import "SongListCell.h"
#import"SongModel.h"
#import "MJRefresh.h"
#import "MJRefreshFooterView.h"
#import "MJRefreshHeaderView.h"
#import "singleSong.h"
#import"PlayListViewController.h"
#import"MBProgressHUD.h"
#import"PlayListViewController.h"
@interface SongListViewController ()
{
    NSInteger number;
    NSUInteger temp;
}
@property(nonatomic,retain)UITableView *tableview;
@property(nonatomic,retain)NSMutableArray *arr;
@property(nonatomic,retain)NSMutableArray *arrModel;
@property(nonatomic,retain)MBProgressHUD *HUD;
@end

@implementation SongListViewController
- (void)dealloc
{
    [_tableview release];
    [_arr release];
    [_arrModel release];
    [_HUD release];
    [super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self){
        number = 1;
        temp = 1;
        self.arr=[[NSMutableArray alloc]init];
        self.arrModel=[[NSMutableArray alloc]init];
    }
    return self;
}


- (void)viewDidLoad
{
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bujing"]];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"播放" style:UIBarButtonItemStyleDone target:self action:@selector(actionBuLand)];
     self.navigationItem.rightBarButtonItem.tintColor=[UIColor whiteColor];
    
    UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width * 150 / 375, self.view.frame.size.height * 10 / 667, self.view.frame.size.width * 50 / 375, self.view.frame.size.height * 30 / 667)];
    titleLabel.text=self.str;
    [self.view addSubview:titleLabel];
    [titleLabel release];
    
    UIButton *buBack=[UIButton buttonWithType:UIButtonTypeSystem];
    buBack.frame=CGRectMake(0, self.view.frame.size.height * 15 / 667, self.view.frame.size.width * 40 / 375, self.view.frame.size.height * 30 / 667);
     [self.view addSubview:buBack];
    
    [self data];
    
    self.tableview =[[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-self.view.frame.size.height * 120 / 667) style:UITableViewStylePlain];
    self.tableview.backgroundColor = [UIColor clearColor];
    self.tableview.rowHeight=100;
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    [self.view addSubview:self.tableview];
    [self addFooter];

    [_tableview release];
    //小菊花     //方式1.直接在View上show
    _HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
    
    
    //常用的设置
    //小矩形的背景色
    _HUD.color = [UIColor clearColor];//这儿表示无背景
    //显示的文字
    _HUD.labelText = @"提示";
    //细节文字
    _HUD.detailsLabelText = @"正在加载内容";
    //是否有庶罩
    _HUD.dimBackground = YES;


    [_tableview release];

}
//size=50
- (void)data
{
    NSNumber*strurl=[self.dic objectForKey:@"songlist_id"];
    
    NSString *str1 = [NSString stringWithFormat: @"http://api.dongting.com/channel/channel/%@/songs?size=50&page=",strurl];
//    NSLog(@"11111111%@",str1);
    if (number <= temp) {
        NSString *str2 = [NSString stringWithFormat:@"%ld&app=ttpod&v=v7.7.0.2015012818&uid=&mid=iPhone5S&f=f320&s=s310&imsi=&hid=&splus=7.1&active=1&net=2&openudid=f6194442ca8980c5d85b989cf3d3dc0a4377fda1&idfa=5AA671CE-591C-4ED9-9D2B-A3E90383B9F1&utdid=VTDTxsGQAW8DAMDeSyjEEAeS&alf=201200", number];
        NSString *str = [NSString stringWithFormat:@"%@%@", str1, str2];
//        NSLog(@"2222%@", str);
        [AFNetworkingTool NetWorkingTooStr:str block:^(id result) {
            
            NSMutableArray*arr1=[NSMutableArray array] ;
            
            arr1=[result objectForKey:@"data"];
            NSMutableDictionary *dic1=[NSMutableDictionary dictionary];
            dic1=result;
            temp = [[dic1 objectForKey:@"pageCount"] integerValue];
            for (int i=0; i<arr1.count; i++) {
                [self.arr addObject:arr1[i]];
            }
            
            for (NSInteger i = 0; i <arr1.count ; i++) {
                SongModel *mo = [[SongModel alloc]init];
                NSMutableDictionary *dic1 = arr1[i];
                mo.song_name=[dic1 objectForKey:@"name"];
                mo.singer_name=[dic1 objectForKey:@"singerName"];
                NSMutableArray *arr = [dic1 objectForKey:@"urlList"];
                
                for (NSDictionary *dic  in arr) {
                    mo.url=[dic objectForKey:@"url"];
                }
                [self.arrModel addObject:mo];
                [mo release];
            }
            [self.tableview reloadData];
            [_HUD hide:YES];
        }];
    }
}

-(void)addFooter
{
    [self.tableview addFooterWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            number++;
            [self data];
            [self.tableview reloadData];
            
            [self.tableview footerEndRefreshing];
        });
        
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

-(UITableViewCell* )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse=@"reuse";
    SongListCell* cell=[tableView dequeueReusableCellWithIdentifier:reuse ];
    if(!cell){
        cell=[[SongListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
    }
    cell.backgroundColor = [UIColor clearColor];
    SongModel *song=self.arrModel[indexPath.row];
    NSString *str = [NSString stringWithFormat:@"%ld.%@",indexPath.row +1,song.song_name];
    cell.selectionStyle = NO;
    cell.songName.text=str;
    cell.singerName.text=song.singer_name;
    return cell;
}
-(void)actionBuLand{
    PlayListViewController *plvc=[[PlayListViewController alloc]init];
    [self.navigationController pushViewController:plvc animated:YES];
    [plvc release];
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [singleSong shareSingleSong].arrSong=self.arrModel;
    [singleSong shareSingleSong].num=indexPath.row;
    
    NSNotificationCenter *center=[NSNotificationCenter  defaultCenter];
    
    [center postNotificationName:@"播放" object:@"收到了看见了" userInfo:nil];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
